//! # 依赖注入模块测试
//! 
//! 本模块包含对依赖注入功能的测试用例。

#[cfg(test)]
mod tests {
    use super::*;
    use crate::di::core::DIContainer;
    use crate::di::traits::{Container, ComponentFactory, ComponentLifecycleManager};
    use crate::di::models::{ComponentMetadata, ComponentLifecycle, ComponentState};
    use crate::di::factory::SimpleFactory;
    use rustcloud_core::{ServiceResult, ServiceError};
    use std::sync::Arc;
    use async_trait::async_trait;

    struct TestService {
        name: String,
    }

    impl TestService {
        fn new(name: String) -> Self {
            Self { name }
        }
    }

    #[async_trait]
    impl ComponentLifecycleManager for TestService {
        async fn initialize(&self) -> ServiceResult<()> {
            tracing::info!("TestService {} initialized", self.name);
            Ok(())
        }

        async fn start(&self) -> ServiceResult<()> {
            tracing::info!("TestService {} started", self.name);
            Ok(())
        }
    }

    #[tokio::test]
    async fn test_di_container() {
        let container = DIContainer::new();

        // 注册组件
        let factory = SimpleFactory::new(|_container| {
            Box::pin(async {
                Ok(Arc::new(TestService::new("test".to_string())))
            })
        });

        container.register_singleton("test_service", factory).await.unwrap();

        // 解析组件
        let service = container.resolve::<TestService>().await.unwrap();
        assert_eq!(service.name, "test");

        // 再次解析应该返回同一个实例
        let service2 = container.resolve::<TestService>().await.unwrap();
        assert!(Arc::ptr_eq(&service, &service2));
    }

    #[tokio::test]
    async fn test_component_lifecycle() {
        let container = DIContainer::new();
        
        let factory = SimpleFactory::new(|_container| {
            Box::pin(async {
                Ok(Arc::new(TestService::new("lifecycle_test".to_string())))
            })
        });

        container.register_singleton("test_service", factory).await.unwrap();

        // 启动容器
        container.start().await.unwrap();

        // 解析组件
        let _service = container.resolve::<TestService>().await.unwrap();

        // 停止容器
        container.stop().await.unwrap();

        assert_eq!(container.get_state().await, ComponentState::Stopped);
    }
}